#region Imports
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using InsightHumans = PHSRAG.Insight21.Humans;  
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// Summary description for ChartNavigation.
	/// </summary>
	[ToolboxData("<{0}:ChartNavigation runat=server />")]
	[Designer(typeof(ChartNavigation), typeof(IDesigner))]
	public class ChartNavigation: SubNavigation
	{
		#region Constants
		private const string selectedChartNavigationLinkKey = 
			"SelectedChartNavigationLink_D6852F33-9A0C-41a9-93B5-F25162639DB7";
		#endregion

		#region Instance Variables
		private LinkButton chartOneLinkButton;
		private LinkButton chartOneCheckListLinkButton;
		private LinkButton chartOneOALinkButton; //Outside Activities
		private LinkButton chartTwoLinkButton;
		private LinkButton chartTwoOILinkButton; //Other Interests
		private LinkButton chartThreeLinkButton;
		private LinkButton chartThreeFALinkButton; //Family's Associations
		#endregion

		#region Implementations for abstract properties and methods from Base Class SubNavigation

		/// <summary>
		/// Create all controls in the ChartNavigation.
		/// </summary>
		protected override void CreateControls()
		{
			Controls.Clear();

			chartOneLinkButton = new LinkButton();
			chartOneLinkButton.CommandName = chartOneLinkButton.Text = "Chart I";
			chartOneLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartURL", "");

			chartOneCheckListLinkButton = new LinkButton();
			chartOneCheckListLinkButton.CommandName = chartOneCheckListLinkButton.Text = "Chart I Checklist";
			chartOneCheckListLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartURL", "");

			chartOneOALinkButton = new LinkButton();
			chartOneOALinkButton.CommandName = chartOneOALinkButton.Text = "Chart I Outside Activities";
			chartOneOALinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartWithSponsorsURL", "");

			chartTwoLinkButton = new LinkButton();
			chartTwoLinkButton.CommandName = chartTwoLinkButton.Text = "Chart II";
			chartTwoLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartURL", "");

			chartTwoOILinkButton = new LinkButton();
			chartTwoOILinkButton.CommandName = chartTwoOILinkButton.Text = "Chart II Other Interests";
			chartTwoOILinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartWithSponsorsURL", "");

			chartThreeLinkButton = new LinkButton();
			chartThreeLinkButton.CommandName = chartThreeLinkButton.Text = "Chart III";
			chartThreeLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartURL", "");

			chartThreeFALinkButton = new LinkButton();
			chartThreeFALinkButton.CommandName = chartThreeFALinkButton.Text = "Chart III - Family's Assocations";
			chartThreeFALinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.AnswerChartWithSponsorsURL", "");

		}

		
		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls 
		/// within the ChartNavigation.
		/// </summary>
		protected override void GenerateControlTree()
		{
			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SubModuleSummaryNavigation";
			table.EnableViewState = false;

			TableRow row = new TableRow();
			table.Rows.Add(row);

			TableCell cell = new TableCell();
			row.Cells.Add(cell);
			cell.CssClass = "SubModuleSummaryNavigationFillerCell";

			table = new Table();
			table.CssClass = "SubModuleSummaryNavigation";
			cell.Controls.Add(table);

			row = new TableRow();
			table.Rows.Add(row);

			TableCell cell2 = new TableCell();
			row.Cells.Add(cell2);
			cell2.Controls.Add(chartOneLinkButton);

			cell2 = new TableCell();
			row.Cells.Add(cell2);
			cell2.Controls.Add(chartTwoLinkButton);

			cell2 = new TableCell();
			row.Cells.Add(cell2);
			cell2.Controls.Add(chartThreeLinkButton);

			CreateSelectedLinkAdornments(row);

//			row = new TableRow();
//			table.Rows.Add(row);
//			cell2 = new TableCell();
//			row.Cells.Add(cell2);
//			cell2.Controls.Add(new LiteralControl("&nbsp;"));

			table = new Table();
			cell.Controls.Add(table);
			
			row = new TableRow();
			table.Rows.Add(row);

			LinkButton selectedLink = GetSelectedLink();
			if (selectedLink.CommandName == chartOneLinkButton.CommandName || selectedLink.CommandName == chartOneCheckListLinkButton.CommandName
										|| selectedLink.CommandName == chartOneOALinkButton.CommandName)
			{
				
				cell2 = new TableCell();
				row.Cells.Add(cell2);
				cell2.Controls.Add(chartOneCheckListLinkButton);

				cell2 = new TableCell();
				row.Cells.Add(cell2);
				cell2.Controls.Add(chartOneOALinkButton);
			}
			else if(selectedLink.CommandName == chartTwoLinkButton.CommandName || selectedLink.CommandName == chartTwoOILinkButton.CommandName)
			{
				cell2 = new TableCell();
				row.Cells.Add(cell2);
				cell2.Controls.Add(chartTwoOILinkButton);
			}
			else if (selectedLink.CommandName == chartThreeLinkButton.CommandName || selectedLink.CommandName == chartThreeFALinkButton.CommandName)
			{
				cell2 = new TableCell();
				row.Cells.Add(cell2);
				cell2.Controls.Add(chartThreeFALinkButton);
			}
			CreateSelectedLinkAdornments(row);
		}
		

		/// <summary>
		/// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
		/// cell is of a different style.
		/// </summary>
		private void CreateSelectedLinkAdornments(TableRow row)
		{
			LinkButton selectedLink = GetSelectedLink();
			
			LinkButton parentLink = null;
			if (selectedLink.CommandName == chartOneCheckListLinkButton.CommandName || selectedLink.CommandName == chartOneOALinkButton.CommandName)
				parentLink = chartOneLinkButton;
			if (selectedLink.CommandName == chartTwoOILinkButton.CommandName)
				parentLink = chartTwoLinkButton;
			if (selectedLink.CommandName == chartThreeFALinkButton.CommandName)
				parentLink = chartThreeLinkButton;

			if (selectedLink != null)
			{
				foreach (TableCell cell in row.Cells)
				{
					if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink || (parentLink != null && cell.Controls[0] == parentLink))) 
					{
						cell.CssClass = "TabNavigationSelectedSummary";

//						TableCell adornmentCell = new TableCell();
//						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
//						row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

//						const string prefixImage = "../Images/tab_Left.gif";
//						System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
//						Image image = new Image();
//						image.ImageUrl = prefixImage;
//						adornmentCell.Width = bitmap.Width;
//						adornmentCell.Controls.Add(image);

//						adornmentCell = new TableCell();
//						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
//						row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

//						const string suffixImage = "../Images/tab_Right.gif";
//						bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
//						image = new System.Web.UI.WebControls.Image();
//						image.ImageUrl = suffixImage;
//						adornmentCell.Width = bitmap.Width;
//						adornmentCell.Controls.Add(image);

						break;
					}
				}
			}
		}

		/// <summary>
		/// Retrieve the name of the link that was last clicked on.
		/// </summary>
		/// <returns>Reference to the link button corresponding to the clicked link</returns>
		protected override LinkButton GetSelectedLink()
		{
			// Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link 
			// in the session. Retrieve that name and return the corresponding link button reference.

			object o = Page.Session[SelectedSubNavigationLinkKey];
			if (o != null)
			{
				string commandName = (string)o;
				if (commandName == chartOneLinkButton.CommandName)
				{
					return chartOneLinkButton;
				}
				if (commandName == chartOneCheckListLinkButton.CommandName)
				{
					return chartOneCheckListLinkButton;
				}
				if (commandName == chartOneOALinkButton.CommandName)
				{
					return chartOneOALinkButton;
				}
				if (commandName == chartTwoLinkButton.CommandName)
				{
					return chartTwoLinkButton;
				}
				if (commandName == chartTwoOILinkButton.CommandName)
				{
					return chartTwoOILinkButton;
				}
				if (commandName == chartThreeLinkButton.CommandName)
				{
					return chartThreeLinkButton;
				}
				if (commandName == chartThreeFALinkButton.CommandName)
				{
					return chartThreeFALinkButton;
				}
			}
			return chartOneLinkButton;
		}

		/// <summary>
		/// 
		/// </summary>
		protected override string SelectedSubNavigationLinkKey
		{
			get 
			{
				return selectedChartNavigationLinkKey;
			}
		}
		/// <summary>
		/// Set the selected link so that on load, the appropriate tab may be shown highlighted. This property is invoked
		/// by pages while vectoring to other pages which host this navigation control.
		/// </summary>
		public string SelectedLink
		{
			set { Page.Session[SelectedSubNavigationLinkKey] = value; }
		}
		
		#endregion


		#region Class ChartNavigationDesigner
		/// <summary>
		/// Designer associated with the control.
		/// </summary>
		public class ChartNavigationDesigner : ControlDesigner
		{
			#region Overrides
			/// <summary>
			/// Force the control to create its child controls for display at design time.
			/// </summary>
			/// <returns>HTML representation of the control</returns>
			public override string GetDesignTimeHtml()
			{
				((ChartNavigation)Component).Design();
				return base.GetDesignTimeHtml();
			}
			#endregion
		}
		#endregion
	}
}
